﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Xml.Linq;
using System.Web.DynamicData;
using System.Text;
using System.Data.Linq;

namespace Administration
{
    public partial class _Default : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            Menu1.DataSource = MetaModel.GetModel(typeof(WofkflowDataContext)).VisibleTables;
            Menu1.DataBind();
            //----------------------
            Menu2.DataSource = MetaModel.GetModel(typeof(LookupsDataContext)).VisibleTables;
            Menu2.DataBind();
        }

        protected void GenerateScript(MetaModel mode)
        {
            StringBuilder strbld = new StringBuilder();
            foreach (var table in mode.VisibleTables)
            {
                DataContext context = (DataContext) table.CreateContext();
                foreach (var row in table.GetQuery(context))
                {
                    Utils.Data.SqlDom.InsertSqlStatment insert = new Utils.Data.SqlDom.InsertSqlStatment();
                    insert.Values.UseInlineValues = true;
                    insert.Table.TableName = context.Mapping.GetTable(table.EntityType).TableName.Remove(0, 4);

                    foreach (var column in table.Columns)
                    {
                        if (column.EntityTypeProperty.PropertyType.IsValueType || column.EntityTypeProperty.PropertyType == typeof(string))
                        {
                            var result = column.EntityTypeProperty.GetValue(row, null);
                            if (result != null)
                                insert.Values.Add(column.Name, result);
                        }
                    }

                    strbld.AppendLine(insert.ToString());
                    strbld.AppendLine("GO");
                    strbld.AppendLine();
                }
                context.Dispose();
            }
            DownloadFile(strbld.ToString());
        }

        private void DownloadFile(string file)
        {
            Response.Clear();
            //-------------------
            Response.Write(file);
            Response.ContentType = "text/sql";
            Response.AddHeader("Content-Disposition", "attachment; filename=" + "script.sql");
            Response.AddHeader("Content-Length", Encoding.Unicode.GetByteCount(file).ToString());

            Response.End();
        }

        protected void btnLKgenerateScript(object sender, EventArgs e)
        {
            GenerateScript(MetaModel.GetModel(typeof(LookupsDataContext)));
        }

        protected void btnWFgenerateScript(object sender, EventArgs e)
        {
            GenerateScript(MetaModel.GetModel(typeof(WofkflowDataContext)));
        }
    }
}
